package com.paic.mhis.hcpms.quota.exportword.biz.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.paic.mhis.hcpms.common.document.util.Constant;
import com.paic.mhis.hcpms.quota.exportword.biz.service.MedPerFeeWordReportService;
import com.paic.mhis.hcpms.quota.exportword.dao.WordDataDao;
import com.paic.mhis.hcpms.quota.exportword.dto.IndicatorWordDTO;

/**
* 获取各模块中 平均住院床日 ，每床日费用， 门诊人数 ，人均费用 的值
* 
 * @author: ex-zhengchao001
* @version:
*/
@Service("medPerFeeWordReportService")
public class MedPerFeeWordReportServiceImpl implements MedPerFeeWordReportService {
	private static final Logger LOGGER = Logger.getLogger(WordReportServiceImpl.class);

	@Autowired
	private WordDataDao wordDataDao;

	private String checkRation(String chainRatio, String periodRatio, Map<String, Object> map) {
		if (null == map.get("mom_lower")) {
			return "  ";
		}
		BigDecimal chain = new BigDecimal(chainRatio);
		BigDecimal period = new BigDecimal(periodRatio);
		if (chain.compareTo(new BigDecimal(map.get("mom_lower").toString())) >= 0
				&& chain.compareTo(new BigDecimal(map.get("mom_upper").toString())) <= 0
				&& period.compareTo(new BigDecimal(map.get("yoy_lower").toString())) >= 0
				&& period.compareTo(new BigDecimal(map.get("yoy_upper").toString())) <= 0) {
			return "无明显异常";
		}
		return "可能存在异常";
	}

	private BigDecimal ratioBigDecimal2Zero(BigDecimal bigDec) {
		if (bigDec.compareTo(new BigDecimal(0)) == 0) {
			return new BigDecimal(1);
		}
		return bigDec;
	}

	private String num2MathABS(String str) {
		return new BigDecimal(str).abs() + "";
	}

	private Map<String, Object> setRatioMap(String kpiCode, Map<String, Object> kpiLimitValue) {
		Map<String, Object> ratioMap = new HashMap<String, Object>();
		ratioMap.put("yoy_upper", kpiLimitValue.get(kpiCode + "_yoy_upper_end"));
		ratioMap.put("yoy_lower", kpiLimitValue.get(kpiCode + "_yoy_lower_end"));
		ratioMap.put("mom_upper", kpiLimitValue.get(kpiCode + "_mom_upper_end"));
		ratioMap.put("mom_lower", kpiLimitValue.get(kpiCode + "_mom_lower_end"));

		return ratioMap;
	}

	private String checkOneRation(String ratio, Map<String, Object> map, int type) {

		if (null == map.get("mom_upper") || null == map.get("yoy_upper") || null == map.get("mom_lower")
				|| null == map.get("yoy_lower")) {
			return "  ";
		}

		BigDecimal upRatio = null;
		BigDecimal downRatio = null;
		switch (type) {
		case 1:
			upRatio = new BigDecimal(map.get("yoy_upper").toString());
			downRatio = new BigDecimal(map.get("yoy_lower").toString());
			break;
		default:
			upRatio = new BigDecimal(map.get("mom_upper").toString());
			downRatio = new BigDecimal(map.get("mom_lower").toString());
			break;
		}
		BigDecimal temp = new BigDecimal(ratio);
		if (temp.compareTo(downRatio) >= 0 && temp.compareTo(upRatio) <= 0) {
			return "无明显异常";
		}
		return "可能存在异常";
	}

	private String checkToZero(String ratio) {
		BigDecimal temp = new BigDecimal(ratio);
		if (temp.compareTo(new BigDecimal(0)) >= 0) {
			return "增加";
		}
		return "降低";
	}

	private String checkFlag(String flag) {
		if ("  ".equals(flag)) {
			return "  ";
		}
		if ("无明显异常".equals(flag)) {
			return "未超出";
		}
		return "已超出";
	}

	// 计算当前月的上一个月
	private String lastDate(String date) {
		date = date.replace("/", "-");
		String[] dates = date.split("-");
		int year = Integer.parseInt(dates[0]);
		int month = Integer.parseInt(dates[1]);
		if (month == Constant.MONTH_FIRST) {
			month = Constant.MONTH_TWELVE;
			year -= Constant.MONTH_FIRST;
		} else {
			month -= Constant.MONTH_FIRST;
		}
		String nextDate = year + "-" + (month < Constant.MONTH_TEN ? "0" + month : month);
		return nextDate;
	}

	// 计算去年今月
	private String lastYearToday(String date) {
		date = date.replace("/", "-");
		String[] dates = date.split("-");
		int year = Integer.parseInt(dates[0]) - Constant.MONTH_FIRST;
		String nextDate = year + "-" + dates[1];
		return nextDate;
	}

	// Map添加参数抽出的方法
	private Map<String, String> setMapParment(String date, String kpiCode, String visitType, String sociationCode) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("date", date);
		map.put("kpiCode", kpiCode);
		map.put("visitType", visitType);
		map.put("sociationCode", sociationCode);
		return map;
	}

	// 冒泡排序
	private List<IndicatorWordDTO> bubbleSortAlgorithm(List<IndicatorWordDTO> list) {
		if (list.isEmpty() || null == list) {
			return list;
		}
		for (int n = 0; n < list.size() - 1; n++) {
			for (int m = 1; m < list.size() - n; m++) {
				IndicatorWordDTO a = new IndicatorWordDTO();
				if ((Double.valueOf(list.get(m - 1).getKpiValue()))
						.compareTo(Double.valueOf(list.get(m).getKpiValue())) < 0) {
					a = list.get(m - 1);
					list.set((m - 1), list.get(m));
					list.set(m, a);
				}
			}
		}

		return list;
	}

	/**
	* 查询单一机构中 科室费用分析 visitType 1 ,2 用来控制是门诊还是住院 1: 住院 2：门诊
	* 
	 * @param mapValue
	* @return
	*/
	@Override
	public Map<String, Object> querySingleInstitutionsDeptCostAnalysis(Map<String, Object> mapValue, String visitType) {
		Map<String, Object> map = new HashMap<String, Object>();
		DecimalFormat df = new DecimalFormat("0.00");
		String date = mapValue.get("currentDate").toString();
		String name = mapValue.get("medicalOrgCode").toString();
		String sociationCode = mapValue.get("regionCode").toString();
		Map<String, String> mapParment = setMapParment(date, "KPI0001", visitType, sociationCode);
		mapParment.put("healthcareProviderCode", name);
		try {
			List<IndicatorWordDTO> list = wordDataDao.queryWorkDept(mapParment);

			list = bubbleSortAlgorithm(list);

			List<String[]> deptValueList = new ArrayList<String[]>();

			List<String[]> pieList = new ArrayList<String[]>();
			BigDecimal sum = BigDecimal.ZERO;
			BigDecimal topTen = BigDecimal.ZERO;
			int i = 0;
			for (IndicatorWordDTO dto : list) {
				String[] temp = new String[4];
				String[] pie = new String[4];
				pie[0] = temp[0] = dto.getWorkDept();
				pie[1] = temp[1] = df.format(new BigDecimal(dto.getKpiValue()));
				temp[2] = df.format(new BigDecimal(dto.getPeriodRatio()));
				temp[3] = df.format(new BigDecimal(dto.getChainRatio()));
				deptValueList.add(temp);
				if (i < 9) {
					pieList.add(pie);
					topTen = topTen.add(new BigDecimal(dto.getKpiValue()));
					i++;
				}
				sum = sum.add(new BigDecimal(dto.getKpiValue()));
			}

			String[] other = new String[4];
			other[0] = "其他";
			other[1] = df.format(sum.subtract(topTen));
			if (i >= 9) {
				pieList.add(other);
			}
			String[] all = new String[4];
			all[0] = "合计";
			all[1] = df.format(sum);
			deptValueList.add(all);
			if ("1".equals(visitType)) {
				map.put("k2_2_5_1_tableList_end", deptValueList);
				map.put("k2_2_5_1_pieList_end", pieList);
			} else {
				map.put("k2_3_5_1_tableList_end", deptValueList);
				map.put("k2_3_5_1_pieList_end", pieList);
			}
		} catch (Exception e) {
			LOGGER.error("查询单一机构中 科室费用分析：", e);
		}
		return map;
	}

	/**
	* 查询多等级机构一年的门诊人数和次均费用
	* 
	 * @param mapValue
	* @return map
	*/
	@Override
	public Map<String, Object> queryMoreOrgHospCostAndTimesNumList(Map<String, Object> mapValue) {
		DecimalFormat df = new DecimalFormat("0.00");
		DecimalFormat dfNum = new DecimalFormat("0");
		Map<String, Object> map = new HashMap<String, Object>();
		String date = mapValue.get("currentDate").toString();
		String sociationCode = mapValue.get("regionCode").toString();
		String visitType = "2";
		try {
			String[] lv3 = queryMorOrg(mapValue, "2");
			String[] lv2 = queryMorOrg(mapValue, "3");
			String[] lv1 = queryMorOrg(mapValue, "4");
			String[] lv0 = new String[7];
			lv0[0] = "合计";
			lv0[1] = dfNum.format(wordDataDao.queryAllValue(this.setMapParment(date, "KPI0039", visitType,
					sociationCode)));
			lv0[4] = df
					.format(wordDataDao.queryAllValue(this.setMapParment(date, "KPI0052", visitType, sociationCode)));
			List<String[]> list = new ArrayList<String[]>();
			list.add(lv3);
			list.add(lv2);
			list.add(lv1);
			list.add(lv0);
			map.put("k1_3_4_1_tableList_end", list);
		} catch (Exception e) {
			LOGGER.error("查询多等级机构一年的门诊人数和次均费用：", e);
		}
		return map;
	}

	/**
	* grade="2"三级 grade="3"二级 grade="4"一级
	* 
	 * @param mapValue
	* @param grade
	* @return
	*/
	private String[] queryMorOrg(Map<String, Object> mapValue, String grade) {
		DecimalFormat df = new DecimalFormat("0.00");
		DecimalFormat dfNum = new DecimalFormat("0");

		String[] lv = new String[7];
		if ("2".equals(grade)) {
			lv[0] = "三级医院";
		} else if ("3".equals(grade)) {
			lv[0] = "二级医院";
		} else if ("4".equals(grade)) {
			lv[0] = "一级及以下医院";
		}
		String date = mapValue.get("currentDate").toString();
		String sociationCode = mapValue.get("regionCode").toString();
		// 0039人数
		String kpiCode = "KPI0039";
		Map<String, String> mapParment = setMapParment(date, kpiCode, "2", sociationCode);
		mapParment.put("grade", grade);
		try {
			
			List<IndicatorWordDTO> list =dealList(wordDataDao.queryOrgData(mapParment), "month", date);
			lv[1]=dfNum.format(new BigDecimal(list.get(0).getKpiValue()));	
			lv[2]=df.format(new BigDecimal(list.get(0).getPeriodRatio()));	
			lv[3]=df.format(new BigDecimal(list.get(0).getChainRatio()));	
			
			mapParment.put("kpiCode", "KPI0052");
			
			list =dealList(wordDataDao.queryOrgData(mapParment), "month", date);
			lv[4]=df.format(new BigDecimal(list.get(0).getKpiValue()));	
			lv[5]=df.format(new BigDecimal(list.get(0).getPeriodRatio()));	
			lv[6]=df.format(new BigDecimal(list.get(0).getChainRatio()));	
			
			
//			BigDecimal perNum = wordDataDao.queryMoreOrgOutData(mapParment);
//			lv[1] = dfNum.format(perNum);
//			mapParment.put("date", lastDate(date));
//			BigDecimal lastPerNum = wordDataDao.queryMoreOrgOutData(mapParment);
//			lv[3] = num2MathABS(df.format(perNum.subtract(lastPerNum).divide(ratioBigDecimal2Zero(lastPerNum), 2,
//					BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100))));
//			mapParment.put("date", lastYearToday(date));
//			BigDecimal lastYearPerNum = wordDataDao.queryMoreOrgOutData(mapParment);
//			lv[2] = num2MathABS(df.format(perNum.subtract(lastYearPerNum).divide(ratioBigDecimal2Zero(lastYearPerNum),
//					2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100))));
//			// 0052 费用
//			mapParment.put("kpiCode", "KPI0052");
//			mapParment.put("date", date);
//			BigDecimal fee = wordDataDao.queryMoreOrgOutData(mapParment);
//			lv[4] = num2MathABS(df.format(fee));
//			mapParment.put("date", lastDate(date));
//			BigDecimal lastFee = wordDataDao.queryMoreOrgOutData(mapParment);
//			lv[6] = num2MathABS(df.format(fee.subtract(lastFee).divide(ratioBigDecimal2Zero(lastFee), 2,
//					BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100))));
//			mapParment.put("date", lastYearToday(date));
//			BigDecimal lastYearFee = wordDataDao.queryMoreOrgOutData(mapParment);
//			lv[5] = num2MathABS(df.format(fee.subtract(lastYearFee).divide(ratioBigDecimal2Zero(lastYearFee), 2,
//					BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100))));
//		
			} catch (Exception e) {
			LOGGER.error("查询多机构等级：", e);
		}
		return lv;
	}

	/**
	* 查询机构当前月份的门诊人数和次均费用
	* 
	 * @param mapValue
	* @return
	*/
	@Override
	public Map<String, Object> queryOrgHospCostAndTimesNum(Map<String, Object> mapValue,
			Map<String, Object> kpiLimitValue) {
		DecimalFormat df = new DecimalFormat("0.00");

		DecimalFormat dfFee = new DecimalFormat("0");

		String date = mapValue.get("currentDate").toString();
		String sociationCode = mapValue.get("regionCode").toString();
		String visitType = mapValue.get("visitType").toString();

		// TODO
		Map<String, Object> map39 = countRatioYear(date, "KPI0039", sociationCode, visitType,null);
		Map<String, Object> map52 = countRatioYear(date, "KPI0052", sociationCode, visitType,null);

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> mapParment = setMapParment(date, "KPI0039", visitType, sociationCode);

		dealMap(map, "0039", map39, "0052", map52, "k0_");

		/**
		*  k0_0039_momL_end
		*  k0_0039_yoyL_end
		*  k0_0039_valueL_end
		*  k0_0039_threeValueL_end
		*  
		 *  k0_0052_momL_end
		*  k0_0052_yoyL_end
		*  k0_0052_valueL_end
		*  k0_0052_threeValueL_end
		*  
		 */

		try {
			List<IndicatorWordDTO> list = wordDataDao.queryKpiData(mapParment);
			list = dealList(list, "month", date);
			BigDecimal times = new BigDecimal(list.get(0).getKpiValue());
			map.put("k0_3_4_1_times_end", dfFee.format(times));
			map.put("k0_3_4_1_times_health_flag_end",
					checkRation(list.get(0).getChainRatio(), list.get(0).getPeriodRatio(),
							setRatioMap("KPI0039", kpiLimitValue)));
			map.put("k0_3_4_1_yoy_flag_end", checkToZero(list.get(0).getPeriodRatio()));
			map.put("k0_3_4_1_yoy_end", num2MathABS(df.format(new BigDecimal(list.get(0).getPeriodRatio()))));
			map.put("k0_3_4_1_times_result_end",
					checkFlag(checkOneRation(list.get(0).getPeriodRatio(), this.setRatioMap("KPI0039", kpiLimitValue),
							1)));
			map.put("k0_3_4_1_mom_flag_end", checkToZero(df.format(new BigDecimal(list.get(0).getChainRatio()))));
			map.put("k0_3_4_1_mom_end", num2MathABS(df.format(new BigDecimal(list.get(0).getChainRatio()))));
			map.put("k0_3_4_1_mom_result_end",
					checkFlag(checkOneRation(list.get(0).getChainRatio(), this.setRatioMap("KPI0039", kpiLimitValue), 2)));

			mapParment.put("kpiCode", "KPI0052");
			list = wordDataDao.queryKpiData(mapParment);
			list = dealList(list, "month", date);

			IndicatorWordDTO iwDto = list.get(0);
			BigDecimal avgFee = new BigDecimal(iwDto.getKpiValue());
			// BigDecimal[] ration = calculatePeriodRatioAndChainRatio(avgFee,
			// mapParment);

			map.put("k0_3_4_2_fee_end", df.format(avgFee));
			map.put("k0_3_4_2_fee_health_flag_end",
					checkRation(iwDto.getChainRatio(), iwDto.getPeriodRatio(), setRatioMap("KPI0052", kpiLimitValue)));
			map.put("k0_3_4_2_yoy_flag_end", checkToZero(iwDto.getPeriodRatio()));
			map.put("k0_3_4_2_yoy_end", num2MathABS(df.format(new BigDecimal(iwDto.getPeriodRatio()))));
			map.put("k0_3_4_2_yoy_result_end",
					checkFlag(checkOneRation(iwDto.getPeriodRatio(), this.setRatioMap("KPI0052", kpiLimitValue), 1)));
			map.put("k0_3_4_2_mom_flag_end", checkToZero(list.get(0).getChainRatio()));
			map.put("k0_3_4_2_mom_end", num2MathABS(df.format(new BigDecimal(iwDto.getChainRatio()))));
			map.put("k0_3_4_2_mom_result_end",
					checkFlag(checkOneRation(iwDto.getChainRatio(), this.setRatioMap("KPI0052", kpiLimitValue), 2)));
		} catch (Exception e) {
			LOGGER.error("解析queryOrgHospCostAndTimesNum：", e);
		}

		return setRatioYoyAndMom(map, kpiLimitValue, "KPI0052", "KPI0039");
	}

	// 空集合的处理以及填充集合数据中的空值
	private List<IndicatorWordDTO> dealList(List<IndicatorWordDTO> list, String type, String date) {
		String begDate = countBeginDate(date);
		if (list == null || list.isEmpty()) {
			list = new ArrayList<IndicatorWordDTO>();
			if ("year".equals(type)) {
				for (int i = 0; i < Constant.MONTH_TWELVE; i++) {
					IndicatorWordDTO iwDTO = new IndicatorWordDTO();
					iwDTO.setDate(begDate);
					list.add(iwDTO);
					begDate = monthAddOne(begDate);
				}
			} else {
				IndicatorWordDTO iwDTO = new IndicatorWordDTO();
				iwDTO.setDate(date);
				list.add(iwDTO);
			}
		} else {
			if ("year".equals(type)) {
				List<IndicatorWordDTO> listTemp = new ArrayList<IndicatorWordDTO>();
				List<String> dates = new ArrayList<String>();
				for (int i = 0; i < Constant.MONTH_TWELVE; i++) {
					dates.add(monthAddOne(begDate, i));
					IndicatorWordDTO iwDTO = new IndicatorWordDTO();
					iwDTO.setDate(dates.get(i));
					listTemp.add(iwDTO);
				}
				setListTemp(list, listTemp, dates);
				return listTemp;
			}
		}
		return list;
	}

	private void setListTemp(List<IndicatorWordDTO> list, List<IndicatorWordDTO> listTemp, List<String> dates) {
		String temp = null;
		for (int i = 0; i < list.size(); i++) {
			int num = -Constant.MONTH_FIRST;
			temp = list.get(i).getDate();
			for (String s : dates) {
				num++;
				if (temp.equals(s)) {
					listTemp.get(num).setChainRatio(list.get(i).getChainRatio());
					listTemp.get(num).setKpiValue(list.get(i).getKpiValue());
					listTemp.get(num).setPeriodRatio(list.get(i).getPeriodRatio());
				}
			}

		}
	}

	// 计算当前月的下 num 个月
	private String monthAddOne(String date, int num) {
		String retDate = "";
		for (int i = 0; i <= num; i++) {
			if (i == 0) {
				retDate = date;
			} else {
				retDate = monthAddOne(retDate);
			}
		}
		return retDate;
	}

	// 计算当前月的下一个月
	private String monthAddOne(String date) {
		date = date.replace("/", "-");
		String[] dates = date.split("-");
		int year = Integer.parseInt(dates[0]);
		int month = Integer.parseInt(dates[1]);
		if (++month > Constant.MONTH_TWELVE) {
			month = Constant.MONTH_FIRST;
			year += Constant.MONTH_FIRST;
		}
		String nextDate = year + "-" + (month < Constant.MONTH_TEN ? "0" + month : month);
		return nextDate;
	}

	// 计算时间方法----计算当前的时间的前12个月
	private String countBeginDate(String str) {
		str = str.replace("/", "-");
		String[] date = str.split("-");
		int year = Integer.parseInt(date[0]);
		int month = Integer.parseInt(date[1]);

		if (month == Constant.MONTH_TWELVE) {
			month = Constant.MONTH_FIRST;
		} else {
			year -= Constant.MONTH_FIRST;
			month += Constant.MONTH_FIRST;
		}
		String begin = year + "-" + (month < Constant.MONTH_TEN ? "0" + month : month);
		return begin;
	}

	/**
	* 查询总体情况门诊人数和次均费用
	* 
	 * @param mapValue
	* @return
	*/
	@Override
	public Map<String, Object> queryGeneralOutpatientNumberAndTimesCostList(Map<String, Object> mapValue) {
		DecimalFormat df = new DecimalFormat("0.00");
		DecimalFormat dfFee = new DecimalFormat("0");
		String date = mapValue.get("currentDate").toString();
		String sociationCode = mapValue.get("regionCode").toString();
		String visitType = mapValue.get("visitType").toString();
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> mapParment = setMapParment(date, "KPI0039", visitType, sociationCode);
		mapParment.put("beginDate", countBeginDate(date));
		List<String> value0010 = new ArrayList<String>();
		List<String> value0001 = new ArrayList<String>();
		try {
			List<IndicatorWordDTO> listNum = dealList(wordDataDao.queryOutYearData(mapParment), "year", date);
			mapParment.put("kpiCode", "KPI0052");
			List<IndicatorWordDTO> listFee = dealList(wordDataDao.queryOutYearData(mapParment), "year", date);
			for (int i = 0; i < Constant.MONTH_TWELVE; i++) {
				BigDecimal num = new BigDecimal(listNum.get(i).getKpiValue());
				value0010.add(dfFee.format(num));

				value0001.add(df.format(new BigDecimal(listFee.get(i).getKpiValue())));

			}
		} catch (Exception e) {
			LOGGER.error("解析queryGeneralOutpatientNumberAndTimesCostList：", e);
		}
		map.put("k0_3_4_2_fee_zyList_end", value0010);
		map.put("k0_3_4_2_fee_crList_end", value0001);
		return map;
	}

	/**
	* 查询单一机构当前月份门诊人数和次均费用
	* 
	 * @param mapValue
	* @return
	*/
	@Override
	public Map<String, Object> querySingleAgencyOutpatientNumAndTimesCost(Map<String, Object> mapValue,
			Map<String, Object> kpiVal) {
		String date = mapValue.get("currentDate").toString();
		String name = mapValue.get("medicalOrgCode").toString();
		String sociationCode = mapValue.get("regionCode").toString();
		String visitType = mapValue.get("visitType").toString();

		// TODO
		Map<String, Object> map39 = countRatioYear(date, "KPI0039", sociationCode, visitType,name);
		Map<String, Object> map52 = countRatioYear(date, "KPI0052", sociationCode, visitType,name);
		
		DecimalFormat df = new DecimalFormat("0.00");
		DecimalFormat dfFee = new DecimalFormat("0");
		Map<String, Object> map = new HashMap<String, Object>();

		dealMap(map, "0039", map39, "0052", map52, "k2_");
		/**
		*  k2_0039_momL_end
		*  k2_0039_yoyL_end
		*  k2_0039_valueL_end
		*  k2_0039_threeValueL_end
		*  
		 *  k2_0052_momL_end
		*  k2_0052_yoyL_end
		*  k2_0052_valueL_end
		*  k2_0052_threeValueL_end
		*  
		 */

		Map<String, String> mapParment = setMapParment(date, "KPI0039", visitType, sociationCode);
		mapParment.put("healthcareProviderCode", name);
		try {

			List<IndicatorWordDTO> listNum = dealList(wordDataDao.queryOneOrgOutFeeData(mapParment), "month", date);
			map.put("k2_3_4_1_days_end", dfFee.format(new BigDecimal(listNum.get(0).getKpiValue())));
			map.put("k2_3_4_1_days_health_flag_end",
					checkRation(df.format(new BigDecimal(listNum.get(0).getChainRatio())),
							df.format(new BigDecimal(listNum.get(0).getPeriodRatio())), setRatioMap("KPI0039", kpiVal)));

			map.put("k2_3_4_1_yoy_flag_end", checkToZero(df.format(new BigDecimal(listNum.get(0).getPeriodRatio()))));
			map.put("k2_3_4_1_yoy_end", num2MathABS(df.format(new BigDecimal(listNum.get(0).getPeriodRatio()))));
			map.put("k2_3_4_1_yoy_result_end",
					checkFlag(checkOneRation(df.format(new BigDecimal(listNum.get(0).getPeriodRatio())),
							setRatioMap("KPI0039", kpiVal), 1)));

			map.put("k2_3_4_1_mom_flag_end", checkToZero(df.format(new BigDecimal(listNum.get(0).getChainRatio()))));
			map.put("k2_3_4_1_mom_end", num2MathABS(df.format(new BigDecimal(listNum.get(0).getChainRatio()))));
			map.put("k2_3_4_1_mom_result_end",
					checkFlag(checkOneRation(df.format(new BigDecimal(listNum.get(0).getChainRatio())),
							setRatioMap("KPI0039", kpiVal), 2)));

			mapParment.put("kpiCode", "KPI0052");
			mapParment.put("date", date);
			List<IndicatorWordDTO> listFee = dealList(wordDataDao.queryOneOrgOutFeeData(mapParment), "month", date);
			// 人均费用
			map.put("k2_3_4_2_fee_end", num2MathABS(df.format(new BigDecimal(listFee.get(0).getKpiValue()))));
			map.put("k2_3_4_2_fee_health_flag_end",
					checkRation(df.format(new BigDecimal(listFee.get(0).getPeriodRatio())),
							df.format(new BigDecimal(listFee.get(0).getChainRatio())), setRatioMap("KPI0052", kpiVal)));

			// 同比
			map.put("k2_3_4_2_yoy_flag_end", checkToZero(df.format(new BigDecimal(listFee.get(0).getPeriodRatio()))));
			map.put("k2_3_4_2_yoy_end", num2MathABS(df.format(new BigDecimal(listFee.get(0).getPeriodRatio()))));
			map.put("k2_3_4_2_yoy_result_end",
					checkFlag(checkOneRation(df.format(new BigDecimal(listFee.get(0).getPeriodRatio())),
							setRatioMap("KPI0052", kpiVal), 1)));

			// 环比
			map.put("k2_3_4_2_mom_flag_end", checkToZero(df.format(new BigDecimal(listFee.get(0).getChainRatio()))));
			map.put("k2_3_4_2_fee_mom_end", num2MathABS(df.format(new BigDecimal(listFee.get(0).getChainRatio()))));
			map.put("k2_3_4_2_mom_result_end",
					checkFlag(checkOneRation(df.format(new BigDecimal(listFee.get(0).getChainRatio())),
							setRatioMap("KPI0052", kpiVal), 2)));
		} catch (Exception e) {
			LOGGER.error("解析querySingleAgencyOutpatientNumAndTimesCost：", e);
		}

		return setRatioYoyAndMom(map, kpiVal, "KPI0052", "KPI0039");
	}

	/**
	* 查询单一机构一年门诊人数和次均费用
	* 
	 * @param mapValue
	* @return
	*/
	@Override
	public Map<String, Object> querySingleAgencyOutpatientNumAndTimesCostList(Map<String, Object> mapValue,
			Map<String, Object> kpiLimitValue) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> listPer = new ArrayList<String>();
		List<String> listFee = new ArrayList<String>();
		List<String> listDate = new ArrayList<String>();
		String date = mapValue.get("currentDate").toString();
		String temp = mapValue.get("currentDate").toString();
		try {
			for (int i = 0; i < Constant.MONTH_TWELVE; i++) {
				Map<String, Object> mapPerment = querySingleAgencyOutpatientNumAndTimesCost(mapValue, kpiLimitValue);
				listPer.add(mapPerment.get("k2_3_4_1_days_end").toString());
				listFee.add(mapPerment.get("k2_3_4_2_fee_end").toString());
				listDate.add(date);
				date = lastDate(date);
				mapValue.put("currentDate", date);
			}
			Collections.reverse(listPer);
			Collections.reverse(listFee);

			mapValue.put("currentDate", temp);
			map.put("k2_3_4_2_fee_zyList_end", listPer);
			map.put("k2_3_4_2_fee_crList_end", listFee);
			map.put("k2_3_4_2_fee_date_end", listDate);
		} catch (Exception e) {
			mapValue.put("currentDate", temp);
			LOGGER.error("解析querySingleAgencyOutpatientNumAndTimesCostList：", e);
		}
		return map;
	}

	/**
	* 查询某月月均住院日和 每日费用
	* 
	 * @param mapValue
	* @return
	*/
	@Override
	public Map<String, Object> queryOverallAvgHospBedDaysAndEveBedFee(Map<String, Object> mapValue,
			Map<String, Object> kpiLimitValue) {
		String date = mapValue.get("currentDate").toString();
		String sociationCode = mapValue.get("regionCode").toString();
		String visitType = mapValue.get("visitType").toString();
		// TODO
		Map<String, Object> map6 = countRatioYear(date, "KPI0006", sociationCode, visitType,null);
		Map<String, Object> map7 = countRatioYear(date, "KPI0007", sociationCode, visitType,null);

		DecimalFormat df = new DecimalFormat("0.00");
		DecimalFormat dfFee = new DecimalFormat("0.0");
		Map<String, Object> map = new HashMap<String, Object>();

		dealMap(map, "0006", map6, "0007", map7, "k0_");
		/**
		*  k0_0006_momL_end
		*  k0_0006_yoyL_end
		*  k0_0006_valueL_end
		*  k0_0006_threeValueL_end
		*  
		 *  k0_0007_momL_end
		*  k0_0007_yoyL_end
		*  k0_0007_valueL_end
		*  k0_0007_threeValueL_end
		*  
		 */

		Map<String, String> mapParment = setMapParment(date, "KPI0006", visitType, sociationCode);
		try {
			List<IndicatorWordDTO> list = wordDataDao.queryKpiData(mapParment);
			list = dealList(list, "month", date);

			map.put("k0_2_4_1_days_end", dfFee.format(new BigDecimal(list.get(0).getKpiValue())));
			map.put("k0_2_4_1_days_health_flag_end",
					checkRation(list.get(0).getChainRatio(), list.get(0).getPeriodRatio(),
							setRatioMap("KPI0006", kpiLimitValue)));
			map.put("k0_2_4_1_yoy_flag_end", checkToZero(list.get(0).getPeriodRatio()));
			map.put("k0_2_4_1_yoy_end", num2MathABS(df.format(new BigDecimal(list.get(0).getPeriodRatio()))));
			map.put("k0_2_4_1_yoy_result_end",
					checkFlag(checkOneRation(list.get(0).getPeriodRatio(), this.setRatioMap("KPI0006", kpiLimitValue),
							1)));
			map.put("k0_2_4_1_mom_flag_end", checkToZero(list.get(0).getChainRatio()));
			map.put("k0_2_4_1_mom_end", num2MathABS(df.format(new BigDecimal(list.get(0).getChainRatio()))));
			map.put("k0_2_4_1_mom_result_end",
					checkFlag(checkOneRation(list.get(0).getChainRatio(), this.setRatioMap("KPI0006", kpiLimitValue), 2)));

			mapParment.put("kpiCode", "KPI0007");
			list = wordDataDao.queryKpiData(mapParment);
			list = dealList(list, "month", date);

			map.put("k0_2_4_2_fee_end", df.format(new BigDecimal(list.get(0).getKpiValue())));
			map.put("k0_2_4_2_fee_health_flag_end",
					checkRation(list.get(0).getChainRatio(), list.get(0).getPeriodRatio(),
							setRatioMap("KPI0007", kpiLimitValue)));
			map.put("k0_2_4_2_yoy_flag_end", checkToZero(list.get(0).getPeriodRatio()));
			map.put("k0_2_4_2_yoy_end", num2MathABS(df.format(new BigDecimal(list.get(0).getPeriodRatio()))));
			map.put("k0_2_4_2_yoy_result_end",
					checkFlag(checkOneRation(list.get(0).getPeriodRatio(), this.setRatioMap("KPI0007", kpiLimitValue),
							1)));
			map.put("k0_2_4_2_fee_mom_flag_end", checkToZero(list.get(0).getChainRatio()));
			map.put("k0_2_4_2_fee_mom_end", num2MathABS(df.format(new BigDecimal(list.get(0).getChainRatio()))));
			map.put("k0_2_4_2_mom_result_end",
					checkFlag(checkOneRation(list.get(0).getChainRatio(), this.setRatioMap("KPI0007", kpiLimitValue), 2)));
		} catch (Exception e) {
			LOGGER.error("解析querySingleAgencyOutpatientNumAndTimesCostList：", e);
		}

		return this.setRatioYoyAndMom(map, kpiLimitValue, "KPI0006", "KPI0007");
	}

	private Object checkKpiLimitValue(Object obj) {
		if (null != obj) {
			return obj;
		}

		return "   ";
	}

	private Map<String, Object> setRatioYoyAndMom(Map<String, Object> map, Map<String, Object> kpiLimitValue,
			String kpiCode, String kpiCode2) {

		try {
			map.put(kpiCode + "_yoy_upper_end", checkKpiLimitValue(kpiLimitValue.get(kpiCode + "_yoy_upper_end")));
			map.put(kpiCode + "_yoy_lower_end", checkKpiLimitValue(kpiLimitValue.get(kpiCode + "_yoy_lower_end")));
			map.put(kpiCode + "_mom_upper_end", checkKpiLimitValue(kpiLimitValue.get(kpiCode + "_mom_upper_end")));
			map.put(kpiCode + "_mom_lower_end", checkKpiLimitValue(kpiLimitValue.get(kpiCode + "_mom_lower_end")));
			map.put(kpiCode2 + "_yoy_upper_end", checkKpiLimitValue(kpiLimitValue.get(kpiCode2 + "_yoy_upper_end")));
			map.put(kpiCode2 + "_yoy_lower_end", checkKpiLimitValue(kpiLimitValue.get(kpiCode2 + "_yoy_lower_end")));
			map.put(kpiCode2 + "_mom_upper_end", checkKpiLimitValue(kpiLimitValue.get(kpiCode2 + "_mom_upper_end")));
			map.put(kpiCode2 + "_mom_lower_end", checkKpiLimitValue(kpiLimitValue.get(kpiCode2 + "_mom_lower_end")));
		} catch (Exception e) {
			LOGGER.error("解析setRatioYoyAndMom：", e);
		}
		return map;

	}

	/**
	* 获取12个月 总 平均住院日与日均床日费
	* 
	 * @param mapValue
	* @return
	*/
	@Override
	public Map<String, Object> queryOverallAvgHospBedDaysAndEveBedFeeList(Map<String, Object> mapValue) {

		String date = mapValue.get("currentDate").toString();
		String sociationCode = mapValue.get("regionCode").toString();
		String visitType = mapValue.get("visitType").toString();
		DecimalFormat df = new DecimalFormat("0.00");
		DecimalFormat dfFee = new DecimalFormat("0.0");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> mapParment = setMapParment(date, "KPI0006", visitType, sociationCode);
			mapParment.put("bgeinDate", countBeginDate(date));
			List<IndicatorWordDTO> list = dealList(wordDataDao.queryYearKpiData(mapParment), "year", date);
			List<String> value0006 = new ArrayList<String>();
			for (IndicatorWordDTO i : list) {
				value0006.add(dfFee.format(new BigDecimal(i.getKpiValue())));
			}

			map.put("k0_2_4_2_fee_zyList_end", value0006);
			
			mapParment.put("kpiCode", "KPI0007");
			list = dealList(wordDataDao.queryYearKpiData(mapParment), "year", date);
			List<String> value0007 = new ArrayList<String>();
			for (IndicatorWordDTO i : list) {
				value0007.add(df.format(new BigDecimal(i.getKpiValue())));
			}
			map.put("k0_2_4_2_fee_crList_end", value0007);
		} catch (Exception e) {
			LOGGER.error("解析queryOverallAvgHospBedDaysAndEveBedFeeList：", e);
		}
		return map;
	}

	/**
	* 查询各级机构直接的月均住院日和 每日费用对比
	* 
	 * @param mapValue
	* @return
	*/
	@Override
	public Map<String, Object> queryAvgHospBedDaysAndEveBedCostsCompared(Map<String, Object> mapValue) {
		String date = mapValue.get("currentDate").toString();
		String sociationCode = mapValue.get("regionCode").toString();
		DecimalFormat df = new DecimalFormat("0.00");
		DecimalFormat dfFee = new DecimalFormat("0.0");
		String visitType = "1";
		List<String[]> listLV = new ArrayList<String[]>();
		String[] lv3 = new String[7];
		String[] lv2 = new String[7];
		String[] lv1 = new String[7];
		String[] lv0 = new String[7];
		lv3[0] = "三级医院";
		lv2[0] = "二级医院";
		lv1[0] = "一级及以下医院";
		lv0[0] = "合计";
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> mapParment = setMapParment(date, "KPI0006", visitType, sociationCode);
		mapParment.put("grade", "2");// 三级<====>2
		try {
			List<IndicatorWordDTO> list2 = wordDataDao.queryOrgData(mapParment);
			list2 = dealList(list2, "month", date);
			lv3[1] = dfFee.format(new BigDecimal(list2.get(0).getKpiValue()));
			lv3[2] = df.format(new BigDecimal(list2.get(0).getPeriodRatio()));
			lv3[3] = df.format(new BigDecimal(list2.get(0).getChainRatio()));

			mapParment.put("kpiCode", "KPI0007");
			list2 = wordDataDao.queryOrgData(mapParment);
			list2 = dealList(list2, "month", date);
			lv3[4] = df.format(new BigDecimal(list2.get(0).getKpiValue()));
			lv3[5] = df.format(new BigDecimal(list2.get(0).getPeriodRatio()));
			lv3[6] = df.format(new BigDecimal(list2.get(0).getChainRatio()));

			mapParment.put("grade", "3");// 二级<====>3
			mapParment.put("kpiCode", "KPI0006");
			List<IndicatorWordDTO> list3 = wordDataDao.queryOrgData(mapParment);
			list3 = dealList(list3, "month", date);
			lv2[1] = dfFee.format(new BigDecimal(list3.get(0).getKpiValue()));
			lv2[2] = df.format(new BigDecimal(list3.get(0).getPeriodRatio()));
			lv2[3] = df.format(new BigDecimal(list3.get(0).getChainRatio()));
			mapParment.put("kpiCode", "KPI0007");
			list3 = wordDataDao.queryOrgData(mapParment);
			list3 = dealList(list3, "month", date);
			lv2[4] = df.format(new BigDecimal(list3.get(0).getKpiValue()));
			lv2[5] = df.format(new BigDecimal(list3.get(0).getPeriodRatio()));
			lv2[6] = df.format(new BigDecimal(list3.get(0).getChainRatio()));

			mapParment.put("grade", "4");// 一级<====>4
			mapParment.put("kpiCode", "KPI0006");
			List<IndicatorWordDTO> list4 = wordDataDao.queryOrgData(mapParment);
			list4 = dealList(list4, "month", date);
			lv1[1] = dfFee.format(new BigDecimal(list4.get(0).getKpiValue()));
			lv1[2] = df.format(new BigDecimal(list4.get(0).getPeriodRatio()));
			lv1[3] = df.format(new BigDecimal(list4.get(0).getChainRatio()));
			mapParment.put("kpiCode", "KPI0007"); 
			list4 = wordDataDao.queryOrgData(mapParment);
			list4 = dealList(list4, "month", date);
			lv1[4] = df.format(new BigDecimal(list4.get(0).getKpiValue()));
			lv1[5] = df.format(new BigDecimal(list4.get(0).getPeriodRatio()));
			lv1[6] = df.format(new BigDecimal(list4.get(0).getChainRatio()));
			lv0[1] = dfFee
					.format(wordDataDao.queryAllValue(this.setMapParment(date, "KPI0006", visitType, sociationCode)));
			lv0[4] = df
					.format(wordDataDao.queryAllValue(this.setMapParment(date, "KPI0007", visitType, sociationCode)));
			listLV.add(lv3);
			listLV.add(lv2);
			listLV.add(lv1);
			listLV.add(lv0);
			map.put("k1_2_4_1_tableList_end", listLV);
		} catch (Exception e) {
			LOGGER.error("解析queryOverallAvgHospBedDaysAndEveBedFeeList：", e);
		}
		return map;
	}

	@Override
	public Map<String, Object> queryFundRisk(Map<String, Object> mapValue) {
		Map<String, Object> map = new HashMap<String, Object>();
		return map;
	}

	@Override
	public Map<String, Object> queryFundRatioGDP(Map<String, Object> mapValue) {
		Map<String, Object> map = new HashMap<String, Object>();
		List fundList = new ArrayList(12);
		List empList = new ArrayList(12);
		List perList = new ArrayList(12);
		map.put("fund", fundList);
		map.put("emp", empList);
		map.put("per", perList);
		return map;
	}

	/**
	* 查询单一机构某月的月均住院日和 每日费用
	* 
	 * @param mapValue
	* @return
	*/
	@Override
	public Map<String, Object> querySingleAvgHospBedDaysAndEveBedFee(Map<String, Object> mapValue,
			Map<String, Object> kpiLimitValue) {
		String date = mapValue.get("currentDate").toString();
		String name = mapValue.get("medicalOrgCode").toString();
		String sociationCode = mapValue.get("regionCode").toString();
		String visitType = mapValue.get("visitType").toString();
		Map<String, Object> map = new HashMap<String, Object>();
		// TODO
		Map<String, Object> map6 = countRatioYear(date, "KPI0006", sociationCode, visitType,name);
		Map<String, Object> map7 = countRatioYear(date, "KPI0007", sociationCode, visitType,name);

		dealMap(map, "0006", map6, "0007", map7, "k2_");
		/**
		*  k2_0006_momL_end
		*  k2_0006_yoyL_end
		*  k2_0006_valueL_end
		*  k2_0006_threeValueL_end
		*  
		 *  k2_0007_momL_end
		*  k2_0007_yoyL_end
		*  k2_0007_valueL_end
		*  k2_0007_threeValueL_end
		*  
		 */

		DecimalFormat df = new DecimalFormat("0.00");
		DecimalFormat dfFee = new DecimalFormat("0.0");
		Map<String, String> mapParment = setMapParment(date, "KPI0006", visitType, sociationCode);
		mapParment.put("healthcareProviderCode", name);
		try {
			List<IndicatorWordDTO> list = dealList(wordDataDao.queryOneOrgData(mapParment), "month", date);
			map.put("k2_2_4_1_days_end", dfFee.format(new BigDecimal(list.get(0).getKpiValue())));
			map.put("k2_2_4_1_days_health_flag_end",
					checkRation(list.get(0).getChainRatio(), list.get(0).getPeriodRatio(),
							setRatioMap("KPI0006", kpiLimitValue)));
			map.put("k2_2_4_1_yoy_flag_end", checkToZero(list.get(0).getPeriodRatio()));
			map.put("k2_2_4_1_yoy_end", num2MathABS(df.format(new BigDecimal(list.get(0).getPeriodRatio()))));
			map.put("k2_2_4_1_yoy_result_end",
					checkFlag(checkOneRation(list.get(0).getPeriodRatio(), this.setRatioMap("KPI0006", kpiLimitValue),
							1)));
			map.put("k2_2_4_1_mom_flag_end", checkToZero(list.get(0).getChainRatio()));
			map.put("k2_2_4_1_mom_end", num2MathABS(df.format(new BigDecimal(list.get(0).getChainRatio()))));
			map.put("k2_2_4_1_mom_result_end",
					checkFlag(checkOneRation(list.get(0).getChainRatio(), this.setRatioMap("KPI0006", kpiLimitValue), 2)));

			mapParment.put("kpiCode", "KPI0007");
			list = dealList(wordDataDao.queryOneOrgData(mapParment), "month", date);

			map.put("k2_2_4_2_fee_end", df.format(new BigDecimal(list.get(0).getKpiValue())));
			map.put("k2_2_4_2_fee_health_flag_end",
					checkRation(list.get(0).getChainRatio(), list.get(0).getPeriodRatio(),
							setRatioMap("KPI0007", kpiLimitValue)));
			map.put("k2_2_4_2_yoy_flag_end", checkToZero(list.get(0).getPeriodRatio()));
			map.put("k2_2_4_2_yoy_end", num2MathABS(df.format(new BigDecimal(list.get(0).getPeriodRatio()))));
			map.put("k2_2_4_2_yoy_result_end",
					checkFlag(checkOneRation(list.get(0).getPeriodRatio(), this.setRatioMap("KPI0007", kpiLimitValue),
							1)));
			map.put("k2_2_4_2_fee_mom_flag_end", checkToZero(list.get(0).getChainRatio()));
			map.put("k2_2_4_2_fee_mom_end", num2MathABS(df.format(new BigDecimal(list.get(0).getChainRatio()))));
			map.put("k2_2_4_2_mom_result_end",
					checkFlag(checkOneRation(list.get(0).getChainRatio(), this.setRatioMap("KPI0007", kpiLimitValue), 2)));
		} catch (Exception e) {
			LOGGER.error("解析querySingleAvgHospBedDaysAndEveBedFee：", e);
		}

		return setRatioYoyAndMom(map, kpiLimitValue, "KPI0006", "KPI0007");
	}

	/**
	* 查询单一机构某年的月均住院日和 每日费用
	* 
	 * @param mapValue
	* @return
	*/
	@Override
	public Map<String, Object> querySingleAvgHospBedDaysAndEveBedFeeList(Map<String, Object> mapValue) {
		String date = mapValue.get("currentDate").toString();
		String name = mapValue.get("medicalOrgCode").toString();
		DecimalFormat df = new DecimalFormat("0.00");
		DecimalFormat dfFee = new DecimalFormat("0.0");
		String sociationCode = mapValue.get("regionCode").toString();
		String visitType = mapValue.get("visitType").toString();
		List<String> value0006 = new ArrayList<String>();
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> mapParment = setMapParment(date, "KPI0006", visitType, sociationCode);
		mapParment.put("beginDate", countBeginDate(date));
		mapParment.put("healthcareProviderCode", name);
		try {
			List<IndicatorWordDTO> list = dealList(wordDataDao.queryOneOrgYearData(mapParment), "year", date);
			for (IndicatorWordDTO i : list) {
				value0006.add(dfFee.format(new BigDecimal(i.getKpiValue())));
			}
			map.put("k2_2_4_2_fee_zyList_end", value0006);

			mapParment.put("kpiCode", "KPI0007");
			list = dealList(wordDataDao.queryOneOrgYearData(mapParment), "year", date);
			List<String> value0007 = new ArrayList<String>();
			for (IndicatorWordDTO i : list) {
				value0007.add(df.format(new BigDecimal(i.getKpiValue())));
			}
			map.put("k2_2_4_2_fee_crList_end", value0007);
		} catch (Exception e) {
			LOGGER.error("解析querySingleAvgHospBedDaysAndEveBedFeeList：", e);
		}
		return map;
	}

	List<String> get12list() {
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < 12; i++) {
			list.add(" ");
		}
		return list;
	}

	private Map<String, Object> countRatioYear(String date, String kpiCode, String sociationCode, String visitType,String name) {
		DecimalFormat df = null;
		if ("KPI0006".equals(kpiCode)) {
			df = new DecimalFormat("0.0");
		} else if ("KPI0039".equals(kpiCode)) {
			df = new DecimalFormat("0");
		} else {
			df = new DecimalFormat("0.00");
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> mapValue = new HashMap<String, String>();
		mapValue.put("date", date);
		mapValue.put("kpiCode", kpiCode);
		mapValue.put("sociationCode", sociationCode);
		mapValue.put("visitType", visitType);
		if(null != name){
			mapValue.put("name", name);
			selectData(date, df, map, mapValue , name);
		}else{
			
			selectData(date, df, map, mapValue ,null);
		}
		
		return map;
	}

	private void selectData(String date, DecimalFormat df, Map<String, Object> map, Map<String, String> mapValue,String str) {
		List<String> mom = get12list();
		List<String> yoy = get12list();
		List<String> value = get12list();
		List<String> threeValue = get12list();
		List<IndicatorWordDTO> list = new ArrayList<IndicatorWordDTO>();
		for (int i = 0; i < 12; i++) {
			if(null != str){ 
			     list = wordDataDao.queryRatio(mapValue);
			}else{
				 list = wordDataDao.queryKpiData(mapValue);	
			}
			
			if (list.size() == 0 || list.isEmpty()) {
				IndicatorWordDTO dto = new IndicatorWordDTO();
				dto.setDate(mapValue.get("date"));
				list.add(dto);
			} 

			mom.set(11 - i, df.format(Double.parseDouble(list.get(0).getChainRatio())));
			yoy.set(11 - i, df.format(Double.parseDouble(list.get(0).getPeriodRatio())));
			value.set(11 - i, df.format(Double.parseDouble(list.get(0).getKpiValue())));
			threeValue.set(11 - i, df.format(Double.parseDouble(threeTrendPic(mapValue, value.get(11 - i)))));

			// mom.add(list.get(0).getChainRatio());
			// yoy.add(list.get(0).getPeriodRatio());
			// value.add(list.get(0).getKpiValue());
			// threeValue.add(threeTrendPic(m apValue,list.get(0).getKpiValue()));
		
			mapValue.put("date",lastDate(mapValue.get("date")));
		}
		map.put("mom", mom);
		map.put("yoy", yoy);
		map.put("value", value);
		map.put("threeValue", threeValue);
	}

	private String threeTrendPic(Map<String, String> mapValue, String value) {
		String date = mapValue.get("date");
		DecimalFormat df = new DecimalFormat("0.00");
		mapValue.put("date", lastYearToday(date));
		List<IndicatorWordDTO> list1 = wordDataDao.queryKpiData(mapValue);
		if (list1.size() == 0 || list1.isEmpty()) {
			IndicatorWordDTO dto = new IndicatorWordDTO();
			dto.setDate(mapValue.get("date"));
			list1.add(dto);
		}
		String lastValue1 = (list1.get(0).getKpiValue() == null ? "0" : list1.get(0).getKpiValue());

		mapValue.put("date", lastYearToday(lastYearToday(date)));

		List<IndicatorWordDTO> list2 = wordDataDao.queryKpiData(mapValue);
		if (list2.size() == 0 || list2.isEmpty()) {
			IndicatorWordDTO dto = new IndicatorWordDTO();
			dto.setDate(mapValue.get("date"));
			list2.add(dto);
		}
		String lastValue2 = (list2.get(0).getKpiValue() == null ? "0" : list2.get(0).getKpiValue());

		double v = (Double.parseDouble(value) + Double.parseDouble(lastValue1) + Double.parseDouble(lastValue2)) / 3;
		
		mapValue.put("date", date);

		return df.format(v);
	}

	private void dealMap(Map<String, Object> map, String kpicode1, Map<String, Object> map1, String kpicode2,
			Map<String, Object> map2, String name) {

		map.put(name + kpicode1 + "_momL_end", map1.get("mom"));
		map.put(name + kpicode1 + "_yoyL_end", map1.get("yoy"));
		map.put(name + kpicode1 + "_valueL_end", map1.get("value"));
		map.put(name + kpicode1 + "_threeValueL_end", map1.get("threeValue"));

		map.put(name + kpicode2 + "_momL_end", map2.get("mom"));
		map.put(name + kpicode2 + "_yoyL_end", map2.get("yoy"));
		map.put(name + kpicode2 + "_valueL_end", map2.get("value"));
		map.put(name + kpicode2 + "_threeValueL_end", map2.get("threeValue"));

	}
}
